package com.op.parcel.manage.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.op.parcel.boot.base.DataBaseConstant;
import com.op.parcel.manage.config.security.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 创建和修改
 *
 * @author xuan🐽
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

	public void insertFill(MetaObject metaObject) {
		Object createBy = getFieldValByName(DataBaseConstant.CREATE_BY, metaObject);
		if (createBy == null) {
			setFieldValByName(DataBaseConstant.CREATE_BY, SecurityUtil.getUsername(), metaObject);
		}

		Object createDate = getFieldValByName(DataBaseConstant.CREATE_DATE, metaObject);
		if (createDate == null) {
			setFieldValByName(DataBaseConstant.CREATE_DATE, LocalDateTime.now(), metaObject);
		}

	}

	@Override
	public void updateFill(MetaObject metaObject) {
		Object updateBy = getFieldValByName(DataBaseConstant.UPDATE_BY, metaObject);
		if (updateBy == null) {
			setFieldValByName(DataBaseConstant.UPDATE_BY, SecurityUtil.getUsername(), metaObject);
		}

		Object updateDate = getFieldValByName(DataBaseConstant.UPDATE_DATE, metaObject);
		if (updateDate == null) {
			setFieldValByName(DataBaseConstant.UPDATE_DATE, LocalDateTime.now(), metaObject);
		}
	}

}
